<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="负责人名称" prop="assigneeName">-->
<!--        <el-input-->
<!--          v-model="queryParams.assigneeName"-->
<!--          placeholder="请输入负责人名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仓库id" prop="wareId">-->
<!--        <el-input-->
<!--          v-model="queryParams.wareId"-->
<!--          placeholder="请输入仓库id"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['storage:purchase:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storage:purchase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storage:purchase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['storage:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id">
        <template #default="scope">
          <span>{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人名称" align="center" prop="assigneeName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="仓库名称" align="center" prop="name" />
      <el-table-column label="总价" align="center" prop="amount" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 1" type="danger">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success">待入库</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="danger">已驳回</el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="primary">已入库</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link  v-if="scope.row.status === 1" type="primary" icon="Edit" @click="handleUpdate(scope.row,2)">通过</el-button>
          <el-button link  v-if="scope.row.status === 1" type="primary" icon="Edit" @click="handleUpdate(scope.row,3)">驳回</el-button>
          <el-button link  v-if="scope.row.status === 2" type="primary" icon="Edit" @click="handleUpdate(scope.row,4)">入库</el-button>
          <el-button link v-if="scope.row.status === 3" type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['storage:purchase:remove']">删除</el-button>
          <el-button link type="primary" icon="Edit" @click="handleSelect(scope.row)">查询详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog v-model="dialogTableVisible" title="采购详情" width="800">
      <el-table :data="gridData">
        <el-table-column property="skuNum" label="编号" width="150">
          <template #default="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column property="skuNum" label="购买数量" width="150" />
        <el-table-column property="skuPrice" label="总价" width="200" />
        <el-table-column property="supplierName" label="供应商名称" />
      </el-table>
    </el-dialog>

    <!-- 添加或修改采购信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="purchaseRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="负责人id" prop="assigneeId">
          <el-input v-model="form.assigneeId" placeholder="请输入负责人id" />
        </el-form-item>
        <el-form-item label="负责人名称" prop="assigneeName">
          <el-input v-model="form.assigneeName" placeholder="请输入负责人名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入优先级" />
        </el-form-item>
        <el-form-item label="仓库id" prop="wareId">
          <el-input v-model="form.wareId" placeholder="请输入仓库id" />
        </el-form-item>
        <el-form-item label="价格" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入价格" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Purchase">
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
  purchaseLists
} from "@/api/storage/purchase";

const { proxy } = getCurrentInstance();

const purchaseList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const PurchaseRequest = ref({
  assigneeName:'',
  startTime:null,
  endTime:null,
});
const gridData = ref([])
const dialogTableVisible = ref(false)
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    assigneeName: null,
    status: null,
    wareId: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询采购信息列表 */
// function getList() {
//   loading.value = true;
//   listPurchase(queryParams.value).then(response => {
//     purchaseList.value = response.data.rows;
//       total.value = response.data.total;
//     loading.value = false;
//   });
// }

/** 查询采购信息列表 */
function getList() {
  loading.value = true;
  purchaseLists(PurchaseRequest.value).then(response => {
    purchaseList.value = response.data.rows;
      total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    assigneeId: null,
    assigneeName: null,
    phone: null,
    priority: null,
    status: null,
    wareId: null,
    amount: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("purchaseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加采购信息";
}

/** 修改按钮操作 */
function handleUpdate(row,statu) {

  reset();
  row.status = statu
  console.log(row)
  updatePurchase(row).then(response => {
      form.value = response.data;`/9`
    title.value = "修改采购信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["purchaseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePurchase(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPurchase(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/**
 * 查看详情
 */
function handleSelect(row) {
  console.log(row)
  gridData.value = row.purchaseDetailResponses
  dialogTableVisible.value = true
  console.log(gridData.value)
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除采购信息编号为"' + _ids + '"的数据项？').then(function() {
    return delPurchase(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('storage/purchase/export', {
    ...queryParams.value
  }, `purchase_${new Date().getTime()}.xlsx`)
}

getList();
</script>
